package edu.ccut.saturn.studio.wfg.wfgmodel;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.search.IObserver;
import edu.ccut.saturn.studio.core.search.SaturnFileSearcher;
import edu.ccut.saturn.studio.core.search.SaturnFileSearcher.SearchResult;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.freemaker.FreemarkerUtils;
import edu.ccut.saturn.studio.core.util.xml.SaturnW3CUtil;
import edu.ccut.saturn.studio.export.createhtml.Parameters;
import edu.ccut.saturn.studio.wfg.dialog.routerdialog.SaturnRouterDialog;
import edu.ccut.saturn.studio.wfg.figure.SaturnFigure;
import edu.ccut.saturn.studio.wfg.model.Shape;

public class SaturnRouterNode extends Shape {
	
	private static final long serialVersionUID = 1L;
	
	private static int number = 1;

	public static final String LINE_NAME_PROPERTY = "line_name_property";
	
	private Image routerPictureSmall = createImage(Activator.SMALL_ROUTER_ICON);
	
	private String polymeMode = "1";

	private String offsetMode = "1";
	
	private List<SaturnRouter> routerList = new ArrayList<SaturnRouter>();
	
	public SaturnRouterNode(){
		super.setName(SaturnName.KEY_NODE_NAME_LUYOU + number++);
		super.setSize(new Dimension(50, 50));
	}
	
	@Override
	public Image getIcon() {
		return Activator.getImage(Images.BIG_ROUTER_ICON);
	}
	
	public Image getSmallIcon() {
		return routerPictureSmall;
	}
	
	public void setSmallIcon(Image routerPictureSmall) {
		this.routerPictureSmall = routerPictureSmall;
	}
	
	public String getPolymeMode() {
		return polymeMode;
	}

	public void setPolymeMode(String polymeMode) {
		this.polymeMode = polymeMode;
	}

	public String getOffsetMode() {
		return offsetMode;
	}

	public void setOffsetMode(String offsetMode) {
		this.offsetMode = offsetMode;
	}

	public List<SaturnRouter> getRouterList() {
		return routerList;
	}

	public void setRouterList(List<SaturnRouter> routerList) {
		this.routerList = routerList;
		firePropertyChange(LINE_NAME_PROPERTY, null, routerList);
	}

	@Override
	public Node doRefresh(Document document) {
		Node routerNode = document.createElement(SaturnName.XML_NAME);
		SaturnW3CUtil.createAttribute(routerNode, SaturnName.XML_TYPE, SaturnName.KEY_NONE_ATTIBUTRE_DECISION );
		SaturnW3CUtil.createChildNode(routerNode, SaturnName.KEY_NODE_ELEMENT_OFFSETMODE, this.offsetMode);
		SaturnW3CUtil.createChildNode(routerNode, SaturnName.KEY_NODE_ELEMENT_POLYMEMODE, this.polymeMode);
		creatNodes(document,routerNode);
		Node root = document.getDocumentElement();
		root.appendChild(routerNode);
		return routerNode;
	}

	@Override
	public Dialog getDialog() {
		SaturnRouterDialog dialog = new SaturnRouterDialog(Display.getDefault().getShells()[0],this);
		return dialog;
	}

	@Override
	public IFigure getFigure() {
		SaturnFigure saturnFigure = new SaturnFigure();
		saturnFigure.setIcon(Activator.getImage(Images.BIG_ROUTER_ICON));
		saturnFigure.setName(super.getName());
		return saturnFigure;
	}

	/**
	 * 当SaturnRouterNode增加一条连线时的操作，为增加的连线配置SaturnRouter。
	 * 
	 * */
	public void setSaturnRouter(Shape shape, String expr, String lineName){
		SaturnRouter router = new SaturnRouter();
		router.setId(shape.getId());
		router.setKey("".equals(expr)?"":expr);
		router.setLineName("".equals(lineName)?Messages.getString("SaturnRouterNode.NoNameLine"):lineName);  //$NON-NLS-2$
		this.routerList.add(router);
	}
	
	public void disSaturnRouter(Shape shape){
		for(int i = 0;i<routerList.size();i++){
			SaturnRouter rou = (SaturnRouter)routerList.get(i);
			if(rou.getId().equals(shape.getId())){
				routerList.remove(i);
			}
		}
	}
	
	public void creatNodes(Document document,Node routerNode){
		for(SaturnRouter router : this.routerList){
			Node node = document.createElement(SaturnName.KEY_NODE_NAME_IF);
			SaturnW3CUtil.createAttribute(node, SaturnName.KEY_NODE_NAME_THEN, router.getId());
			SaturnW3CUtil.createChildNode(node, SaturnName.KEY_NODE_NAME_EXPR, router.getKey());
			SaturnW3CUtil.createChildNode(node, SaturnName.KEY_NODE_NAME_LINENAME ,router.getLineName());
			routerNode.appendChild(node);
		}
	}

	@Override
	protected Node createChildNode(Document document, SaturnAttribute attrib,
			String name) {
		return null;
	}

	@Override
	public Shape cloneShape(Shape shape) {
		SaturnRouterNode saturnRouterNode = (SaturnRouterNode)shape;
		SaturnRouterNode newSaturnRouterNode = new SaturnRouterNode();
		newSaturnRouterNode.setName(saturnRouterNode.getName());
		newSaturnRouterNode.setId(saturnRouterNode.getId());
		newSaturnRouterNode.setNextId(saturnRouterNode.getNextId());
		newSaturnRouterNode.setDescription(saturnRouterNode.getDescription());
		newSaturnRouterNode.setOffsetMode(saturnRouterNode.getOffsetMode());
		newSaturnRouterNode.setPolymeMode(saturnRouterNode.getPolymeMode());
		newSaturnRouterNode.setSmallIcon(saturnRouterNode.getSmallIcon());
		return newSaturnRouterNode;
	}
	
	public List<SearchResult> keyMapInWorkspace(IObserver observer) {
		
		List<SearchResult> list = null;
		String[] suffixes = {"logic"};
		
		try {
			list = SaturnFileSearcher.getValueAndDocumentMapInWorkspace(suffixes , observer, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	
	public List<SearchResult> keyMapInProject(IObserver observer) {
		List<SearchResult> list = null;
		
		try {
			list = SaturnFileSearcher.getValueAndDocumentMapInProject(super.findTheProject(), "logic",
					observer, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public Parameters createNodeHtml(String filePath) {
		
		Map<String, String> infos = new HashMap<String, String>();
		String name = getName();
		Parameters para = new Parameters();

		infos.put("name", getName());
		infos.put("id", getId());
		infos.put("offsettype", "单一分支");
		infos.put("polymtype", transfer(getPolymeMode()));
		infos.put("description", getDescription());

		para.setInfourl(name + ".html");
		para.setInfoname(name);
		para.setDescription(getDescription());
		para.setNodeType(Messages.getString("Wfg.Router")); //$NON-NLS-1$
		para.setImagename("router");
		String templatePath = "template/export/wfg/router";
		String Path = filePath + File.separator + name + ".html";
		FreemarkerUtils.writeTemplateToFile(templatePath, infos, Path);

		return para;
	}
	
	/**
	 * 对聚合模式进行转换
	 * 
	 * @param startPolicy
	 * @return Parameters
	 */
	private static String transfer(String polymeMode) {
		if (polymeMode.equals("1")) {
			return Messages.getString("Wfg.OnePoly"); //$NON-NLS-1$
		} else if (polymeMode.endsWith("2")) {
			return Messages.getString("Wfg.AllPoly"); //$NON-NLS-1$
		} else {
			return Messages.getString("Wfg.ManyPoly"); //$NON-NLS-1$
		}
	}
}